package com.orbitdevs.testgame.input;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.utils.Array;
import com.orbitdevs.testgame.input.TouchScreenRegion;

public class TouchScreen{
	private float SCREENWIDTH;
	private float SCREENHEIGHT;
	private TouchScreenRegion leftRegion;
	private TouchScreenRegion rightRegion;
	public Array<String> events;

	public TouchScreen(float scrW, float scrH) {
		this.SCREENHEIGHT = scrH;
		this.SCREENWIDTH = scrW;
		this.leftRegion = new TouchScreenRegion(SCREENHEIGHT, SCREENWIDTH/2, 0, 0);
		this.rightRegion = new TouchScreenRegion(SCREENHEIGHT, SCREENWIDTH/2, SCREENWIDTH/2, 0);
		this.events = new Array<String>(20);
	}

	public boolean isTouched() {
		return Gdx.input.isTouched();
	}

	public int getDY() {
		return Gdx.input.getDeltaY();
	}

	public int getDX() {
		return Gdx.input.getDeltaX(); // SOSWEETSWAG
	}

	public int getY() {
		return Gdx.input.getY();
	}

	public int getX() {
		return Gdx.input.getX();
	}
	boolean debug = false;
	public void updateTouch(){
		float[] rRec = new float[2];
		float[]	lRec = new float[2];
		if (rightRegion.isTouched()){
			if(!debug){
				Gdx.app.log("Events", "RT touched, recording!");
				debug = true;
			}
			rightRegion.touchRec();
			}
		if(!rightRegion.isTouched() && rightRegion.prevTouched){
			rRec = rightRegion.getTouchVal(true);
			Gdx.app.log("Events", "Recorded movement" + rRec[0] + ", " + rRec[1]);
			generateEvent(rRec, "RIGHT");
			debug = false;
			rightRegion.prevTouched = false;
		}
		if(!rightRegion.isTouched()){
			rightRegion.touched = false;
		}
		
		if(leftRegion.isTouched()){
			leftRegion.touchRec();
			lRec = leftRegion.getTouchVal(false);
			generateEvent(lRec, "LEFT");
		}
		if(!leftRegion.isTouched()){
			generateEvent(lRec, "LEFT");
			leftRegion.clearRec();
		}
		
	}
	
	public void generateEvent(float[] recTouch, String region){
		double angle = Math.toDegrees(Math.atan(recTouch[1]/Math.abs(recTouch[0])));
		double length = Math.sqrt(Math.pow(recTouch[0], 2) + Math.pow(recTouch[1], 2));
		if(region == "RIGHT"){
			if(angle < -60 && length > SCREENHEIGHT / 15){
				events.add("UP");
			}
		}
		if(region == "LEFT"){
			if(recTouch[0] < 0){
				events.add("LEFT");
			}
			if(recTouch[0] > 0){
				events.add("RIGHT");
			}
			if(length < SCREENWIDTH / 30){
				events.add("STOP");
			}
		}
	}

	public void upPress() {
		int x1 = Gdx.input.getX(0);
		int y1 = Gdx.input.getY(0);
		if ((Gdx.input.isTouched() && rightRegion.contains(x1, y1))){
			events.add("UP");
		}
	}

	public boolean leftPress() {
		int x1 = Gdx.input.getX(0);
		int y1 = Gdx.input.getY(0);

		if ((Gdx.input.isTouched(0) || Gdx.input.isTouched(1)) && ((x1 < SCREENWIDTH / 4 && y1 > SCREENHEIGHT / 2))) {
			return true;
		} else {
			return false;
		}
	}

	public boolean rightPress() {
		int x1 = Gdx.input.getX(0);

		int y1 = Gdx.input.getY(0);

		if ((Gdx.input.isTouched(0) || Gdx.input.isTouched(1)) && (x1 > SCREENWIDTH / 4 && x1 < 2 * SCREENWIDTH / 4 && y1 > SCREENHEIGHT / 2)) {
			return true;
		} else {
			return false;
		}
	}
}
